package com.yisu.kabao.manager.website.controller.baseInfo;

import com.yisu.kabao.ewf.common.enums.ResponseCode;
import com.yisu.kabao.ewf.common.web.EasyUIGrid;
import com.yisu.kabao.ewf.common.web.JsonResult;
import com.yisu.kabao.ewf.entity.Advertisement;
import com.yisu.kabao.manager.core.service.IAdvertisementService;
import com.yisu.kabao.manager.domain.request.baseInfoPackage.AdvertisementRequest;
import com.yisu.kabao.manager.website.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;

/**
 * author: 赵杰
 * Business Description：广告位业务
 * createTime：2017/8/10
 */
@RequestMapping(value = "/advertisement")
@Controller
public class AdvertisementController extends BaseController {
    @Resource
    private IAdvertisementService iAdvertisementService;


    /**
     * author: 赵杰
     * Business Description：获取广告位首页
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, HttpServletRequest request, AdvertisementRequest advertisementRequest) throws Exception {
        try {
            if (advertisementRequest.getKeyword() != null) {
                advertisementRequest.setKeyword(URLDecoder.decode(advertisementRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<Advertisement> advertisementEasyUIGrid = iAdvertisementService.getAdvertisementByPage(advertisementRequest);
            model.addAttribute("AdvertisementVos", advertisementEasyUIGrid.getRows());
            model.addAttribute("currentPage", advertisementRequest.getPage());
            model.addAttribute("totalPages", (advertisementRequest.getTotalRecords() / advertisementRequest.getRows()) + 1);
            model.addAttribute("keyword", advertisementRequest.getKeyword());
        } catch (Exception e) {
            logger.error("获取广告位首页业务异常：" + e.getMessage());
        }
        return "/baseInfo/advertisement/index";
    }


    /**
     * author: 赵杰
     * Business Description：广告位编辑页面初始
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify.htm")
    public String modify(Model model, HttpServletRequest request, AdvertisementRequest advertisementRequest) throws Exception {
        Advertisement advertisement = new Advertisement();
        advertisement.setId(Long.valueOf(0));
        try {
            if (advertisementRequest.getId() != 0) {
                advertisement = iAdvertisementService.getAdvertisementModelByPrimaryId(advertisementRequest.getId());
            }
            model.addAttribute("advertisementVo", advertisement);
        } catch (Exception e) {
            logger.error("广告位编辑页面初始异常：" + e.getMessage());
        }
        return "/baseInfo/advertisement/modify";
    }

    /**
     * author: 赵杰
     * Business Description：广告位编辑操作
     *
     * @param advertisement 广告位基本信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyAction(HttpServletRequest request, Advertisement advertisement) {
        try {
            if (iAdvertisementService.modifyAdvertisement(advertisement)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

    /**
     * author: 赵杰
     * Business Description：删除广告位信息
     *
     * @param advertisementRequest 含主键ID
     * @return 状态
     */
    @RequestMapping(value = "/deleteAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteAction(HttpServletRequest request, AdvertisementRequest advertisementRequest) {
        try {
            if (advertisementRequest.getId() == 0) {
                return new JsonResult(ResponseCode.FAILURE.code, "参数不完整！");
            }
            if (iAdvertisementService.deleteAdvertisementByPrimaryId(advertisementRequest.getId())) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(ResponseCode.FAILURE.code, "网络君泡妞去了！");
    }

}
